﻿using System;
using System.IO;
using BepInEx;
using BepInEx.Configuration;
using Fortune.Configuration;
using Fortune.Context;
using UnityEngine;

namespace Fortune.Loader
{
    public static class BepInExConfigLoader
    {
        /// <summary>
        /// 配置文件前缀，作用类似于命名空间
        /// </summary>
        private const string SECTION = "sqlite";
        
        public static void load()
        {
            try
            {
                ConfigFile configFile = new ConfigFile(Path.Combine(Paths.BepInExRootPath, @"plugins\LDBWeb\Resources", FortuneConfiguration.configFileName), false);
                FortuneBepInExConfig fortuneBepInExConfig = new FortuneBepInExConfig
                {
                    dataBasePath = configFile.Bind(SECTION, "dataBasePath", @"BepInEx\plugins\LDBWeb\Resources\LDB_Web.sqlite", "MOD数据库位置，非专业人员请不要擅自改动此配置"),
                    gameVersion = configFile.Bind(SECTION, "gameVersion", GameConfig.gameVersion.ToFullString(), "数据库初始化时的游戏版本，{大版本}.{小版本}.{发布版本}.{构建版本}"),
                    gamePlatform = configFile.Bind(SECTION, "gamePlatform", Enum.GetName(typeof(ESalePlatform), GameMain.data.account.platform), "游戏平台，这个配置暂时没有什么用处"),
                    isSqliteInitialized = configFile.Bind(SECTION, "isSqliteInitialized", false, "数据库是否已初始化"),
                    isStringProtoStored = configFile.Bind(SECTION, "isStringProtoStored", false, "StringProto是否已保存到数据库"),
                    initSqlPath = configFile.Bind(SECTION,  "initSqlPath", @"BepInEx\plugins\LDBWeb\Resources\Sql\DataBaseInit.sql", "数据库初始化sql文件，非专业人员请不要擅自改动此配置")
                };
                // 注册上下文
                FortuneContext.fortuneBepInExConfig = fortuneBepInExConfig;
                FortuneContext.config = configFile;
            }
            catch (Exception e)
            {
                Debug.Log("BepInEx配置文件加载异常");
                Debug.Log(e);
                throw;
            }
        }
    }
}